﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Foothill.Utils;
using GGFW.Entity.DocumentsManage;
using GGFW.Utils.Constants;
using GGFW.DataAccess.DocumentsManage;
using GGFW.DataAccess.Constants;

namespace GGFW.BusinessLogic.DocumentsManage
{
    public class ProductOrderControl
    {
        /// <summary>
        /// 样品数据层访问类
        /// </summary>
        private readonly ProductOrderDao dal = new ProductOrderDao();

        #region 分页查询生产单Grid信息
        /// <summary>
        /// 分页查询生产单信息
        /// </summary>
        /// <param name="PageSize">页面大小</param>
        /// <param name="PageIndex">页序</param>
        /// <param name="queryEntity">样品信息查询实体</param>
        /// <returns>分页数据</returns>
        public FoothillPageData<productgrid> SearchProductGrid(int PageSize, int PageIndex, productgrid productgridEntity)
        {
            FoothillPageData<productgrid> result = new FoothillPageData<productgrid>();

            try
            {   //成功
                int totalCount = 0;
                List<productgrid> models = this.Search_NakederProductGrid(PageSize, PageIndex, productgridEntity, out totalCount);

                result.Records = (models ?? new List<productgrid>());
                result.totalCount = totalCount;
                result.Success = true;
            }
            catch (Exception ex)
            {
                //失败
                GGFW.Utils.Constants.CommonUtil.SealExceptionToFoothillResult(result, ex);
            }

            return result;
        }
        /// <summary>
        /// 生产单信息分页查询，未封装返回结果
        /// </summary>
        /// <param name="PageSize">页面大小</param>
        /// <param name="PageIndex">页序</param>
        /// <param name="queryEntity">样品信息查询实体</param>
        /// <returns>样品信息实体列表</returns>
        public List<productgrid> Search_NakederProductGrid(int PageSize, int PageIndex, productgrid productgridEntity, out int totalCount)
        {
            if (productgridEntity == null)
            {
                totalCount = 0;
                return null;
            }

            return dal.QueryByPageProductGrid(PageSize, PageIndex, productgridEntity, out totalCount);
        }
        #endregion

        #region 保存产品主表信息
        /// <summary>
        /// 保存产品主表信息
        /// </summary>
        /// <param name="productFileds"></param>
        /// <returns></returns>
        public int SaveProduct(string productFileds)
        {
            int pro_id;
            SqlTransaction trans = null;
            SqlConnection conn = null;

            try
            {
                conn = new SqlConnection(SQLConfig.FoothillDataBaseConnectionString);
                conn.Open();
                trans = conn.BeginTransaction();
                //成功             
                pro_id = dal.SaveProduct(productFileds); //新增任务
            }
            catch (Exception ex)
            {
                pro_id = 0;
                if (trans != null)
                {
                    trans.Rollback();
                }
            }
            finally
            {
                trans = null;
                if (conn != null && !ConnectionState.Closed.Equals(conn.State))
                {
                    conn.Close();
                }
            }

            return pro_id;
        }
        #endregion

        #region 保存产品附加表信息
        /// <summary>
        /// 保存产品附加表信息
        /// </summary>
        /// <param name="productaddFileds"></param>
        /// <param name="pro_id"></param>
        /// <returns></returns>
        public bool SaveProductAdd(string productaddFileds, int pro_id)
        {
            SqlTransaction trans = null;
            SqlConnection conn = null;

            try
            {
                conn = new SqlConnection(SQLConfig.FoothillDataBaseConnectionString);
                conn.Open();
                trans = conn.BeginTransaction();
                //成功             
                dal.SaveProductAdd(productaddFileds, pro_id);
            }
            catch (Exception ex)
            {

                if (trans != null)
                {
                    trans.Rollback();
                }
                return false;
            }
            finally
            {
                trans = null;
                if (conn != null && !ConnectionState.Closed.Equals(conn.State))
                {
                    conn.Close();
                }
            }

            return true;
        }
        #endregion

        #region 保存帽款表信息
        /// <summary>
        /// 保存帽款表信息
        /// </summary>
        /// <param name="maokuanFileds"></param>
        /// <param name="pro_id"></param>
        /// <returns></returns>
        public bool SaveMaoKuan(string maokuanFileds, int pro_id)
        {
            SqlTransaction trans = null;
            SqlConnection conn = null;

            try
            {
                conn = new SqlConnection(SQLConfig.FoothillDataBaseConnectionString);
                conn.Open();
                trans = conn.BeginTransaction();
                //成功             
                dal.SaveMaoKuan(maokuanFileds, pro_id);
            }
            catch (Exception ex)
            {

                if (trans != null)
                {
                    trans.Rollback();
                }
                return false;
            }
            finally
            {
                trans = null;
                if (conn != null && !ConnectionState.Closed.Equals(conn.State))
                {
                    conn.Close();
                }
            }

            return true;
        }
        #endregion

        #region 保存包装表信息
        /// <summary>
        /// 保存包装表信息
        /// </summary>
        /// <param name="baozhuangFileds"></param>
        /// <param name="pro_id"></param>
        /// <returns></returns>
        public bool SaveBaoZhuang(string baozhuangFileds, int pro_id)
        {
            SqlTransaction trans = null;
            SqlConnection conn = null;

            try
            {
                conn = new SqlConnection(SQLConfig.FoothillDataBaseConnectionString);
                conn.Open();
                trans = conn.BeginTransaction();
                //成功             
                dal.SaveBaoZhuang(baozhuangFileds, pro_id);
            }
            catch (Exception ex)
            {

                if (trans != null)
                {
                    trans.Rollback();
                }
                return false;
            }
            finally
            {
                trans = null;
                if (conn != null && !ConnectionState.Closed.Equals(conn.State))
                {
                    conn.Close();
                }
            }

            return true;
        }
        #endregion

        #region 保存产品Grid表信息
        /// <summary>
        /// 保存产品Grid表信息
        /// </summary>
        /// <param name="productaddFileds"></param>
        /// <param name="pro_id"></param>
        /// <returns></returns>
        public bool SaveProductGrid(string productgridFileds, int pro_id)
        {
            SqlTransaction trans = null;
            SqlConnection conn = null;

            try
            {
                conn = new SqlConnection(SQLConfig.FoothillDataBaseConnectionString);
                conn.Open();
                trans = conn.BeginTransaction();
                //成功             
                dal.SaveProductGrid(productgridFileds, pro_id);
            }
            catch (Exception ex)
            {

                if (trans != null)
                {
                    trans.Rollback();
                }
                return false;
            }
            finally
            {
                trans = null;
                if (conn != null && !ConnectionState.Closed.Equals(conn.State))
                {
                    conn.Close();
                }
            }

            return true;
        }
        #endregion

        #region 分页查询生产单列表
        /// <summary>
        /// 分页查询生产单列表
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="queryEntity"></param>
        /// <returns></returns>
        public FoothillPageData<product> SearchProductOrderList(int PageSize, int PageIndex, product productqueryEntity)
        {
            FoothillPageData<product> result = new FoothillPageData<product>();

            try
            {   //成功
                int totalCount = 0;
                List<product> models = SearchProductOrderList(PageSize, PageIndex, productqueryEntity, out totalCount);

                result.Records = (models ?? new List<product>());
                result.totalCount = totalCount;
                result.Success = true;
            }
            catch (Exception ex)
            {
                //失败
                GGFW.Utils.Constants.CommonUtil.SealExceptionToFoothillResult(result, ex);
            }

            return result;
        }

        /// <summary>
        /// 获取生产单列表数据
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="productqueryEntity"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<product> SearchProductOrderList(int PageSize, int PageIndex, product productqueryEntity, out int totalCount)
        {
            if (productqueryEntity == null)
            {
                totalCount = 0;
                return null;
            }
            return dal.SearchProductOrderList(PageSize, PageIndex, productqueryEntity, out totalCount);
        }
        #endregion

        #region 删除产品信息
        /// <summary>
        /// 删除产品信息
        /// </summary>
        /// <param name="pro_id">产品ID</param>
        public FoothillOperationResult<product> DelProductByProID(int pro_id)
        {
            FoothillOperationResult<product> result = new FoothillOperationResult<product>();

            try
            {
                dal.DelProductByProID(pro_id);
                result.success = true;
            }
            catch
            {
                result.success = false;
            }

            return result;
        }
        #endregion

        #region 获取产品附加信息(修改网页)
        /// <summary>
        /// 加载产品附加信息
        /// </summary>
        /// <param name="ProID">产品ID</param>
        /// <returns></returns>
        public FoothillOperationResult<v_productinfo> LoadProductInfo(string ProID)
        {
            FoothillOperationResult<v_productinfo> result = new FoothillOperationResult<v_productinfo>();

            try
            {
                //成功
                //result.Data = new List<v_productinfo>();
                //result.Data.Add(dal.LoadProductInfo(ProID));
                result.Data = dal.LoadProductInfo(ProID);
                result.Success = true;
            }
            catch (Exception ex)
            {
                //失败
                GGFW.Utils.Constants.CommonUtil.SealExceptionToFoothillResult(result, ex);
            }

            return result;
        }
        #endregion

        #region 更新产品附加信息
        /// <summary>
        /// 更新产品附加信息
        /// </summary>
        /// <param name="TabName">表名</param>
        /// <param name="FiledName">字段名</param>
        /// <param name="FiledValue">字段值</param>
        /// <returns></returns>
        public FoothillOperationResult<bool> UpdateProductInfo(string ProID, string TabName, string FiledName, string FiledValue)
        {
            FoothillOperationResult<bool> result = new FoothillOperationResult<bool>();

            try
            {
                //成功
                result.Success = dal.UpdateProductInfo(ProID, TabName, FiledName, FiledValue); ;
            }
            catch (Exception ex)
            {
                //失败
                GGFW.Utils.Constants.CommonUtil.SealExceptionToFoothillResult(result, ex);
            }

            return result;
        }

        #endregion

        #region 更新产品
        /// <summary>
        /// 更新产品GRID单个字段函数
        /// </summary>
        /// <param name="PgID"></param>
        /// <param name="FieldName"></param>
        /// <param name="FieldValue"></param>
        /// <returns></returns>
        public FoothillOperationResult<productgrid> UpdateProductGridField(string PgID, string ProID, string FieldName, string FieldValue)
        {
            FoothillOperationResult<productgrid> result = new FoothillOperationResult<productgrid>();

            try
            {
                //成功
                result.Data = new List<productgrid>();
                result.Data.Add(dal.UpdateProductGridField(PgID, ProID, FieldName, FieldValue));
                result.Success = true;
            }
            catch (Exception ex)
            {
                //失败
                GGFW.Utils.Constants.CommonUtil.SealExceptionToFoothillResult(result, ex);
            }

            return result;
        }
        #endregion

        #region 根据关键字分页获取产品信息
        /// <summary>
        /// 根据关键字分页获取产品信息
        /// </summary>
        /// <param name="queryEntity">查询条件</param>
        /// <param name="context">请求</param>
        /// <returns></returns>
        public FoothillPageData<product> searchProductOrder(int pageSize, int pageNo, string queryWord, string type)
        {
            FoothillPageData<product> result = new FoothillPageData<product>();

            try
            {   //成功
                int totalCount = 0;
                List<product> models = Search_NakederProductOrder(pageSize, pageNo, queryWord, type, out totalCount);

                result.Records = (models ?? new List<product>());
                result.totalCount = totalCount;
                result.Success = true;
            }
            catch (Exception ex)
            {
                //失败
                GGFW.Utils.Constants.CommonUtil.SealExceptionToFoothillResult(result, ex);
            }

            return result;
        }

        /// <summary>
        /// 根据批次任务名称进行模糊查找
        /// </summary>
        /// <param name="taskname"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public List<product> Search_NakederProductOrder(int pageSize, int pageNo, string queryWord, string type, out int totalCount)
        {
            return dal.GetProductOrder(pageSize, pageNo, queryWord, type, out totalCount);
        }
        #endregion
    }
}
